<?php
class Direk
{

    /**
     * Construct
     *
     * Opens the database and initializes the object.
     *
     */
    public function __construct()
    {
        $this->db = new SQLiteDatabase( 'db/direk.sqlite' );
    }

    /**
     * Get All Contacts
     *
     * Returns an array containing all contacts.
     *
     * @return array
     */
    public function get_all_contacts()
    {
        $result = $this->db->query( 'SELECT rowid, fname, lname, email, phone FROM contacts ORDER BY lname' );
        if ( $result ) {
            return $result->fetchAll(); 
        } else {
            return false;
        }
    }

    public function get_contact( $id ) 
    {
        $result = $this->db->query( 'SELECT rowid, fname, lname, email, phone FROM contacts WHERE rowid = '.$id );
        if ( $result ) {
            return $result->fetch();
        } else {
            return false;
        }
    }

    /**
     * Create Contact
     *
     * Creates a new entry in the contacts table.
     *
     * @param string $fname First Name
     * @param string $lname Last Name
     * @param string $email Email Address
     * @param string $phone Phone Number
     * @return integer The ROWID of the new entry
     */
    public function create_contact( $fname, $lname, $email, $phone )
    {
        $rc = $this->db->queryExec( 'INSERT INTO contacts VALUES ( "'.sqlite_escape_string( $fname ).'", "'.sqlite_escape_string( $lname ).'", "'.sqlite_escape_string( $email).'", "'.sqlite_escape_string( $phone ).'" )' );
        if ( $rc ) {
            return $this->db->lastInsertRowid();
        } else {
            return false;
        }
    }

    /**
     * Update Contact
     *
     * Updates an existing entry in the contacts table.
     *
     * @param string $fname First Name
     * @param string $lname Last Name
     * @param string $email Email Address
     * @param string $phone Phone Number
     * @return integer The ROWID of the new entry
     */
    public function update_contact( $id, $fname, $lname, $email, $phone )
    {
        $rc = $this->db->queryExec( 'UPDATE contacts SET fname = "'.sqlite_escape_string( $fname ).'", lname = "'.sqlite_escape_string( $lname ).'", email = "'.sqlite_escape_string( $email).'", phone = "'.sqlite_escape_string( $phone ).'" WHERE rowid = '.$id );
        if ( $rc ) {
            return $this->db->lastInsertRowid();
        } else {
            return false;
        }
    }

public function delete_contact( $id ) 
{
    $rc = $this->db->queryExec( 'DELETE FROM contacts WHERE rowid = '.$id );
    if ( $rc ) {
        return true;
    } else {
        return false;
    }


}

/**
 * Debug
 *
 * @param mixed $var
 * @return void
 */
public function debug( $var ) {
    echo '<div class="debug" style="border: 1px dotted black; padding: .5em; font-family: courier, fixed-width; font-size: 10px;    white-space: pre;">';
    echo '<strong>';
    var_export( $var );
    echo '</strong>';
    echo "\n";
    debug_print_backtrace();
    echo '</div>'."\n";
}


}
?>
